package com.txy.webmanagement.service;

import com.gitee.sunchenbin.mybatis.actable.command.PageResultCommand;
import com.gitee.sunchenbin.mybatis.actable.manager.common.BaseMysqlCRUDManager;
import com.txy.webmanagement.model.SysProductType;
import com.txy.webmanagement.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wf
 * @date 2019-03-03$
 **/
@Service
public class SysProductTypeService {
    @Autowired
    private BaseMysqlCRUDManager baseMysqlCRUDManager;

    public List<SysProductType> pageList(SysProductType pd) {
        PageResultCommand<SysProductType> ss = baseMysqlCRUDManager.query(pd);
        List<SysProductType> li = ss.getData();
        return li;
    }

    public SysProductType getOne(String uuid) {
        if (StringUtils.checkStringNotNull(uuid)) {
            SysProductType product = new SysProductType();
            product.setUuid(uuid);
            PageResultCommand<SysProductType> ss = baseMysqlCRUDManager.query(product);
            if (ss.getData() != null && ss.getData().size() > 0) {
                return ss.getData().get(0);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public Boolean save(SysProductType type) {
        SysProductType product = new SysProductType();
        if (StringUtils.checkStringNotNull(type.getUuid())) {
            SysProductType SysProductType = getOne(type.getUuid());
            SysProductType.setParentId(type.getParentId());
            SysProductType.setpCompanyId(type.getpCompanyId());
            SysProductType.setTypeName(type.getTypeName());
            Integer i = baseMysqlCRUDManager.save(SysProductType);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            product.setUuid(StringUtils.getUUID());
            product.setEnable(1);
            Integer i = baseMysqlCRUDManager.save(product);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    public void delete(String uuid) {
        SysProductType type = getOne(uuid);
        if (type != null) {
            baseMysqlCRUDManager.delete(type);
        }
    }
}
